/*
 * Copyright 2011 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.dspl.model;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Strings.nullToEmpty;

import com.google.common.base.Objects;
import com.google.common.collect.Lists;

import java.util.List;
import java.util.Set;

/**
 * Utilities for working with textual information about an element of the dataset.
 * TODO: Add unit tests
 */
public final class Infos {

  /**
   * Ensure a given {@link Info} section has entries for each language.  This method creates empty
   * entries if they are needed.
   */
  public static void ensureTranslations(Info info, Set<String> languages) {
    checkNotNull(info);
    for (String language : languages) {
      String name = nullToEmpty(info.getName(language));
      info.setName(language, name);

      String description = nullToEmpty(info.getDescription(language));
      info.setDescription(language, description);

      String url = nullToEmpty(info.getUrl(language));
      info.setUrl(language, url);
    }
  }

  /**
   * Get the name of a {@link DatasetItem} from its info in a preferred
   * language, or alternatively, less preferred languages if the preferred
   * language is not available. This method allows for a graceful degradation
   * from the user's preferred language through a sequence of alternate
   * languages.
   *
   * @return the name in the preferred language, or one of the secondary
   *         languages, or the empty string if no name can be found.
   */
  public static String getName(Info info, String preferredLanguage, String... languages) {
    String name = info.getName(preferredLanguage);
    if (name != null) {
      return name;
    }

    for (String language : languages) {
      name = info.getName(language);
      if (name != null) {
        return name;
      }
    }

    return "";
  }

  public static boolean sameName(Info info, String otherName,
      String preferredLanguage, String... languages) {
    List<String> orderedLanguages = Lists.newArrayList(languages);
    orderedLanguages.add(0, preferredLanguage);

    for (String language : orderedLanguages) {
      String name = info.getName(language);
      if (Objects.equal(otherName, name)) {
        return true;
      }
    }
    return false;
  }

  /**
   * Get the description of a {@link DatasetItem} from its info in a preferred
   * language, or alternatively, less preferred languages if the preferred
   * language is not available. This method allows for a graceful degradation
   * from the user's preferred language through a sequence of alternate
   * languages.
   *
   * @return the description in the preferred language, or one of the secondary
   *         languages, or the empty string if no name can be found.
   */
  public static String getDescription(Info info, String preferredLanguage, String... languages) {
    String description = info.getDescription(preferredLanguage);
    if (description != null) {
      return description;
    }

    for (String language : languages) {
      description = info.getDescription(language);
      if (description != null) {
        return description;
      }
    }

    return "";
  }

  public static boolean sameDescription(Info info, String otherDescription,
      String preferredLanguage, String... languages) {
    List<String> orderedLanguages = Lists.newArrayList(languages);
    orderedLanguages.add(0, preferredLanguage);

    for (String language : orderedLanguages) {
      String description = info.getDescription(language);
      if (Objects.equal(otherDescription, description)) {
        return true;
      }
    }
    return false;
  }

  /**
   * Get the description of a {@link DatasetItem} from its info in a preferred
   * language, or alternatively, less preferred languages if the preferred
   * language is not available. This method allows for a graceful degradation
   * from the user's preferred language through a sequence of alternate
   * languages.
   *
   * @return the description in the preferred language, or one of the secondary
   *         languages, or the empty string if no name can be found.
   */
  public static String getUrl(Info info, String preferredLanguage, String... languages) {
    String url = info.getUrl(preferredLanguage);
    if (url != null) {
      return url;
    }

    for (String language : languages) {
      url = info.getUrl(language);
      if (url != null) {
        return url;
      }
    }

    return "";
  }

  public static boolean sameUrl(Info info, String otherUrl,
      String preferredLanguage, String... languages) {
    List<String> orderedLanguages = Lists.newArrayList(languages);
    orderedLanguages.add(0, preferredLanguage);

    for (String language : orderedLanguages) {
      String url = info.getUrl(language);
      if (Objects.equal(otherUrl, url)) {
        return true;
      }
    }
    return false;
  }

  private Infos() {
    throw new AssertionError("Do not instantiate.");
  }
}
